# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::wxui::widget::Unit;
use strict;
use utf8;
use Wx;
use base 'Wx::Panel';
use Wx::Event qw(EVT_BUTTON);
use Wx qw(wxHORIZONTAL wxVERTICAL wxEXPAND wxALL wxALIGN_LEFT wxALIGN_RIGHT wxST_NO_AUTORESIZE wxBU_EXACTFIT);

my %do = (1 => 'Разведать', 2 => 'Колонизировать', 3 => 'Выгрузить', 101 => 'Зачистить', 105 => 'Зачистить');
my %undo = (1 => 'Отменить разведку', 2 => 'Отменить колонизацию', 3 => 'Отменить выгрузку', 101 => 'Отменить зачистку', 105 => 'Отменить зачистку');

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $parent = shift;
	my $id = shift;
	my $self = $class->SUPER::new($parent, $id, [-1, -1], [-1, -1]);
	$self->SetBackgroundColour(Wx::Colour->new(0, 0, 0));
	$self->{Unit} = shift;

	my $isWarrior = $self->{Unit}->getProp('is_space_ship') || $self->{Unit}->getProp('is_ground_unit');
	my $name = $self->{Unit}->getDisplayName.($isWarrior ? ' ('.$self->{Unit}->getProp('weight').'т.)' : '');

	$self->{Sizer} = Wx::BoxSizer->new(wxVERTICAL);
	$self->SetSizer($self->{Sizer});
	$self->{Name} = Wx::StaticText->new($self, -1, $name);
	$self->{Name}->SetForegroundColour(Wx::Colour->new(255, 255, 255));
	
	$self->{TopSizer} = Wx::BoxSizer->new(wxHORIZONTAL);
	$self->{TopSizer}->Add($self->{Name}, 1, wxEXPAND, wxALL, 0);

	if ($isWarrior)
	{
		my $hp;
		my $hpColour;
		my $rate = $self->{Unit}->getProp('hit_pointsleft') / $self->{Unit}->getProp('max_hitpoints');
		$rate = 1 if $rate > 1;
		$hpColour = $rate < 1 ? Wx::Colour->new(255, 0, 0) : Wx::Colour->new(255, 255, 255);
		$hp = int($rate * 100).'% ('.($rate < 1 ? $self->{Unit}->getProp('hit_pointsleft').'/': '').$self->{Unit}->getProp('max_hitpoints').')';
		$self->{HitPoints} = Wx::StaticText->new($self, -1, $hp, [-1, -1], [-1, -1], wxALIGN_RIGHT | wxST_NO_AUTORESIZE);
		$self->{HitPoints}->SetForegroundColour($hpColour);
		$self->{TopSizer}->Add($self->{HitPoints}, 0, wxEXPAND, wxALL, 0);
	}

	$self->{Sizer}->Add($self->{TopSizer});

	my $tta = $self->{Unit}->{Parent}->getProp('turns_till_arrival');
	if (defined($tta) && $tta == 0)
	{
		$self->{BottomSizer} = Wx::BoxSizer->new(wxHORIZONTAL);
	
		my $planet = $self->{Unit}->getEmpire()->planets->get(map {$self->{Unit}->{Parent}->getProp($_)} qw(x y));
		my $type = 'Z';
		unless ($planet->getProp('my'))
		{
			if ($planet->getProp('owner_id'))
			{
				my %letters = (-1 => 'U', 0 => 'W', 1 => 'N', 2 => 'A', 3 => 'L', 4 => 'V');
				my $rel = $self->{Unit}->getEmpire()->dipRelation($planet->getProp('owner_id'));
				$rel = -1 unless defined $rel;
				$type = $letters{$rel};
			}
			else
			{
				$type = 'E';
			}
		}
		for my $action (@{$self->{Unit}->getActions()})
		{
			next if $action->{'planet_can_be'} !~ /$type/;
			next if $action->{'action'} == 1 && defined($planet->getProp('s'));
			my $text = defined($action->{'request_id'}) ? $undo{$action->{'action'}} : $do{$action->{'action'}};
			$text = $action->{'action'} unless defined $text;
			$self->{ActionButton}->{$action->{'action'}} = Wx::Button->new($self, -1, $text, [-1, -1], [-1, -1], wxBU_EXACTFIT);
			EVT_BUTTON( $self, $self->{ActionButton}->{$action->{'action'}}->GetId(), sub { $_[0]->toggleAction($action->{'action'}) } );
			$self->{ActionButton}->{$action->{'action'}}->SetForegroundColour(
				defined($action->{'request_id'}) ? Wx::Colour->new(255, 127, 0) : Wx::Colour->new(0, 127, 255)
			);
			$self->{BottomSizer}->Add($self->{ActionButton}->{$action->{'action'}}, 0, wxEXPAND, wxALL, 0);
		}
	
		$self->{Sizer}->Add($self->{BottomSizer});
	}

	return $self;
}

sub toggleAction
{
	my $self = shift;
	my $actionId = shift;
	my ($action) = grep { $_->{'action'} == $actionId } @{$self->{Unit}->getActions()};
	if (defined($action->{'request_id'}))
	{
		$self->{Unit}->cancelAction($actionId);
	}
	else
	{
		$self->{Unit}->performAction($actionId);
	}
	my $text = defined($action->{'request_id'}) ? $undo{$action->{'action'}} : $do{$action->{'action'}};
	$text = $action->{'action'} unless defined $text;
	$self->{ActionButton}->{$action->{'action'}}->SetLabel($text);
	$self->{ActionButton}->{$action->{'action'}}->SetForegroundColour(
		defined($action->{'request_id'}) ? Wx::Colour->new(255, 127, 0) : Wx::Colour->new(0, 127, 255)
	);
	# TODO: Emit Event to send command to the server.
}

sub getUnit
{
	my $self = shift;
	return $self->{Unit};
}

1;
